package server;

import org.apache.mina.core.service.IoHandlerAdapter;
import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.core.session.IoSession;

/**
 * Created by hasee on 2016/9/23.
 */
public class MinaServerHandler  extends IoHandlerAdapter {

    @Override
    /**
     * session创建时
     */
    public void sessionCreated(IoSession session) throws Exception {
        System.out.println("sessionCreate---"+session);
    }

    @Override
    /**
     * session打开时
     */
    public void sessionOpened(IoSession session) throws Exception {

        System.out.println("sessionOpen---"+session);
    }

    @Override
    /**
     * 接收到消息时
     */
    public void messageReceived(IoSession session, Object message) throws Exception {
        String msg = message.toString();
        if(msg.contains("exit")){
            session.write("Bye bye");
        }
        System.out.println("client say:"+msg);
    }

    @Override
    /**
     * 发送消息时
     */
    public void messageSent(IoSession session, Object message) throws Exception {
        System.out.println("server say:"+message);
    }


    @Override
    /**
     * session关闭时
     */
    public void sessionClosed(IoSession session) throws Exception {
        System.out.println("sessionClosed---"+session);
    }

    @Override
    /**
     * session闲置时
     */
    public void sessionIdle(IoSession session, IdleStatus status) throws Exception {
        super.sessionIdle(session, status);
    }

    @Override
    /**
     * 捕捉到异常时
     */
    public void exceptionCaught(IoSession session, Throwable cause) throws Exception {
        super.exceptionCaught(session, cause);
    }

}

